//
//  SetPassWordViewModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/5.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class SetPassWordViewModel: BaseViewModel {
    open var model:SecurityModel? //传递的model
    var isLoadBool:Bool = false //判断是不是登录密码
    var passWord = ""
    var newPassWord = ""
    var codeText = ""
    lazy var request: SetPassWordRequest = {
        let api = SetPassWordRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    lazy var codeRequest:MobileCodeRequest = {
        let api = MobileCodeRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    lazy var emailCodeRequest:EmailCodeRequest = {
        let api = EmailCodeRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    lazy var capitalRequest: CapitalRequest = {
        let api = CapitalRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    
    /// 提交修改密码信息
    ///
    /// - Parameters:
    ///   - success: 成功回调
    ///   - fail: 错误回调
    func putPasswordInformation() {
        
        self.request(request: request, success: {[weak self] (result) in
            CYMBShow.showMessage(kText("密码设置成功"))
            NotificationCenter.default.post(name: NSNotification.Name.Exchange.securityDidChange, object: nil)
            self?.controller?.navigationController?.popViewController(animated: true)
        }) { (request) in
            CYMBShow.showMessage(request.errorMsg)
        }
    }
    
    func putCapital() {
        self.request(request: capitalRequest, success: {[weak self] (result) in
            CYMBShow.showMessage(kText("密码设置成功"))
            NotificationCenter.default.post(name: NSNotification.Name.Exchange.securityDidChange, object: nil)
            self?.controller?.navigationController?.popViewController(animated: true)
        }) { (response) in
            CYMBShow.showMessage(response.errorMsg)
        }
    }
    
    /// 发送验证码
    func sendCode(btn: TimerBtn)  {
        btn.isEnabled = false
        self.request(request: codeRequest, success: { (request) in
            btn.timeStart()
        }) { (request) in
            if Helper.checkTimerCode(response:request.response) {
                btn.timeStart()
            }else {
                btn.isEnabled = true
            }
            CYMBShow.showMessage(request.errorMsg)
        }
    }
    
    func sendEmailCode(btn: TimerBtn) {
        btn.isEnabled = false
        self.request(request: emailCodeRequest, success: { (result) in
            btn.timeStart()
        }) { (request) in
            if Helper.checkTimerCode(response:request.response) {
                btn.timeStart()
            }else {
                btn.isEnabled = true
            }
            CYMBShow.showMessage(request.errorMsg)
        }
    }
    
    func archiveCellKind() {
        var array:[CYTableviewItem] = []
        if self.isLoadBool {
            let itemFir = CYTableviewItem(type: .cyPassWord, name: kText("登录密码"), placeHolder: kText("请输入登录密码"))
            array.append(itemFir)
            let itemSec = CYTableviewItem(type: .cyPassWord, name: kText("确认新密码"), placeHolder: kText("请再次输入登录密码"))
            array.append(itemSec)
            
        }else {
            let itemFir = CYTableviewItem(type: .cyItemNumber, name: kText("设置新密码"), placeHolder: kText("请输入支付密码"))
            array.append(itemFir)
            let itemSec = CYTableviewItem(type: .cyItemNumber, name: kText("确认新密码"), placeHolder: kText("请再次输入支付密码"))
            array.append(itemSec)
        }
        
        
        var itemThird:CYTableviewItem
        if AppSecurityInfo.default.model.isOpenGoogle {
            itemThird = CYTableviewItem(type: .cyItemNumber, name: kText("谷歌验证码"), placeHolder: kText("请输入谷歌验证码"))
        }else if AppSecurityInfo.default.model.isMobileBind{
            itemThird = CYTableviewItem(type: .cyItemTimer, name: kText("手机验证码"), placeHolder: kText("请输入短信验证码"))
        }else {
            itemThird = CYTableviewItem(type: .cyItemTimer, name: kText("邮箱验证码"), placeHolder: kText("请输入邮箱验证码"))
        }
        array.append(itemThird)
        cellfactory.iteamArray.append(array)
    }
    
}
extension SetPassWordViewModel:RequestCallBack,RequestDataReformer,RequestParamSource {
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if successBlock != nil {
            successBlock!(data)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if failBlock != nil {
            failBlock!(request)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? SetPassWordRequest {
            if isLoadBool {
                pramas.updateValue(7, forKey: "codeType")
            }else {
                pramas.updateValue(8, forKey: "codeType")
            }
            pramas.updateValue(passWord, forKey: "password")
            pramas.updateValue(AppSecurityInfo.default.model.areaCode.ch_length != 0 ? AppSecurityInfo.default.model.areaCode as Any : "" , forKey: "areaCode")
            if AppSecurityInfo.default.model.level == bindLevel.googleLevel {
                pramas.updateValue(codeText, forKey: "googleCode")
                pramas.updateValue(2, forKey: "type")
            }else if AppSecurityInfo.default.model.level == bindLevel.iphoneLevel {
                pramas.updateValue(AppSecurityInfo.default.model.mobile.ch_length != 0 ? AppSecurityInfo.default.model.mobile as Any : "", forKey: "mobile")
                pramas.updateValue(codeText, forKey: "mobileCode")
                pramas.updateValue(0, forKey: "type")
            }else if AppSecurityInfo.default.model.level == bindLevel.mailboxLevel {
                pramas.updateValue(AppSecurityInfo.default.model.email.ch_length != 0 ? AppSecurityInfo.default.model.email as Any : "", forKey: "email")
                pramas.updateValue(codeText, forKey: "emailCode")
                pramas.updateValue(1, forKey: "type")
            }

        }else if let _ = request as? MobileCodeRequest {
            if isLoadBool {
                pramas.updateValue(7, forKey: "codeType")
            }else {
                pramas.updateValue(8, forKey: "codeType")
            }
            pramas.updateValue(AppSecurityInfo.default.model.areaCode.ch_length != 0 ? AppSecurityInfo.default.model.areaCode as Any : "" , forKey: "areaCode")
            pramas.updateValue(AppSecurityInfo.default.model.mobile.ch_length != 0 ? AppSecurityInfo.default.model.mobile as Any : "", forKey: "mobile")
        }else if let _ = request as? CapitalRequest {
            pramas.updateValue(8, forKey: "codeType")
            pramas.updateValue(passWord, forKey: "password")
            pramas.updateValue(AppSecurityInfo.default.model.areaCode.ch_length != 0 ? AppSecurityInfo.default.model.areaCode as Any : "" , forKey: "areaCode")
            if AppSecurityInfo.default.model.level == bindLevel.googleLevel {
                pramas.updateValue(codeText, forKey: "googleCode")
                pramas.updateValue(2, forKey: "type")
            }else if AppSecurityInfo.default.model.level == bindLevel.iphoneLevel {
                pramas.updateValue(AppSecurityInfo.default.model.mobile.ch_length != 0 ? AppSecurityInfo.default.model.mobile as Any : "", forKey: "mobile")
                pramas.updateValue(codeText, forKey: "mobileCode")
                pramas.updateValue(0, forKey: "type")
            }else if AppSecurityInfo.default.model.level == bindLevel.mailboxLevel {
                pramas.updateValue(AppSecurityInfo.default.model.email.ch_length != 0 ? AppSecurityInfo.default.model.email as Any : "", forKey: "email")
                pramas.updateValue(codeText, forKey: "emailCode")
                pramas.updateValue(1, forKey: "type")
            }
        }else if let _ = request as? EmailCodeRequest {
            if isLoadBool {
                pramas.updateValue(7, forKey: "codeType")
            }else {
                pramas.updateValue(8, forKey: "codeType")
            }
            pramas.updateValue(1, forKey: "type")
            pramas.updateValue(AppSecurityInfo.default.model.email.ch_length != 0 ? AppSecurityInfo.default.model.email as Any : "", forKey: "email")
            
        }
        
        return pramas
    }
    
    
    
}
